package lumingweihua.future.cn.lumingweihua.home.cheyuan;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import lumingweihua.future.cn.baselibgxh.base.BaseActivity;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;
import lumingweihua.future.cn.baselibgxh.network.JsonElementRequest;
import lumingweihua.future.cn.baselibgxh.network.NetworkManager;
import lumingweihua.future.cn.baselibgxh.network.ReqTag;
import lumingweihua.future.cn.baselibgxh.network.entity.DefaultMamahaoServerError;
import lumingweihua.future.cn.baselibgxh.utils.Constant;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.home.domain.CarData;

/**
 * Created by Administrator on 2018/9/3.
 */

public class DriverInfoActivity extends BaseActivity {
    @Override
    protected void initView() {
        setContentView(R.layout.activity_driver_info);
        initTitleBar("司机信息", Constant.DEFAULT_LEFT_BACK, null);

        findViewById(R.id.bt_ok).setOnClickListener(this);
        findViewById(R.id.bt_cancel).setOnClickListener(this);
    }

    private String good_num;
    private TextView[] et_names;
    private TextView[] et_phones;
    private TextView[] et_licenses;
    private TextView[] et_ids;
    @Override
    protected void initData() {
        //几个信息
        int num = getIntentExtra("num",1);
        good_num = getIntentExtra("good_num");
        et_names = new TextView[num];
        et_phones = new TextView[num];
        et_licenses = new TextView[num];
        et_ids = new TextView[num];
        LinearLayout ll_drivers = findViewById(R.id.ll_drivers);
        for(int i=0;i<num;i++){
            View item = View.inflate(context,R.layout.item_driver,null);
            ((TextView)item.findViewById(R.id.tv_tag)).setText("司机"+(i+1));
            et_names[i] = item.findViewById(R.id.et_name);
            et_phones[i] = item.findViewById(R.id.et_phone);
            et_licenses[i] = item.findViewById(R.id.et_license);
            et_ids[i] = item.findViewById(R.id.et_id);
            ll_drivers.addView(item);
        }
    }

    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.bt_ok:
                for(TextView et_name:et_names){
                    if(TextUtils.isEmpty(et_name.getText())){
                        showToast("请完善司机信息");
                        return;
                    }
                }
                for(TextView et_name:et_phones){
                    if(TextUtils.isEmpty(et_name.getText())){
                        showToast("请完善司机信息");
                        return;
                    }
                }
                for(TextView et_name:et_licenses){
                    if(TextUtils.isEmpty(et_name.getText())){
                        showToast("请完善司机信息");
                        return;
                    }
                }
                for(TextView et_name:et_ids){
                    if(TextUtils.isEmpty(et_name.getText())){
                        showToast("请完善司机信息");
                        return;
                    }
                }

                requestSet();
                break;
            case R.id.bt_cancel:
                finish();
                break;

            default:
                break;
        }
    }

    private final int REQUEST_ROB = 0;
    private final int REQUEST_DRIVER = 1;
    /**
     *
     */
    private void requestSet(){
        showProgressBar(null);

        JsonElementRequest<DefaultMamahaoServerError> req = new JsonElementRequest<DefaultMamahaoServerError>(
                context,"",this);

        req.setParam("apiCode", "_grab_indent_001");
        req.setParam("userToken", MyApplication.getInstance().getUserToken());
        //传这个参数代表修改，没有代表新增地址
        req.setParam("good_num", good_num);
        req.setParam("grab_weight", getIntentExtra("grab_weight"));

        req.setParam("json", getIntentExtra("json"));
//        json  车辆id和车辆装载吨位数传json  例如：[{"id":"1","rough_weight":"10"},{"id":"2","rough_weight":"13"}]


//		//获取token
        String token = NetworkManager.getInstance().getPostToken(req.getParam());
        req.setParam("token", token);

        addRequestQueue(req, REQUEST_ROB, new ReqTag.Builder().handleSimpleRes(true));
    }

    /**
     *
     */
    private void requestDriver(){
        showProgressBar(null);

        JsonElementRequest<DefaultMamahaoServerError> req = new JsonElementRequest<DefaultMamahaoServerError>(
                context,"",this);

        req.setParam("apiCode", "_driver_message_001");
        req.setParam("userToken", MyApplication.getInstance().getUserToken());
        //司机信息
        List<DriverInfo> driverInfos = new ArrayList<>();
        for(int i=0;i<et_names.length;i++){
            driverInfos.add(new DriverInfo(et_ids[i].getText().toString()
                    , et_phones[i].getText().toString()
                    , et_names[i].getText().toString()
                    , good_num
                    ,et_licenses[i].getText().toString()));
        }
        req.setParam("json", new Gson().toJson(driverInfos));
//		//获取token
        String token = NetworkManager.getInstance().getPostToken(req.getParam());
        req.setParam("token", token);

        addRequestQueue(req, REQUEST_DRIVER, new ReqTag.Builder().handleSimpleRes(true));
    }

    @Override
    protected void onResponseSuccess(ReqTag tag, Object data) {
        super.onResponseSuccess(tag, data);
        switch (tag.getReqId()) {
            case REQUEST_ROB:
                //
                requestDriver();
                break;
            case REQUEST_DRIVER:
                new YesOrNoDialog(context, new YesOrNoDialogEntity("抢单信息已通过短信方式发送给货主，请耐心等待货主与您联系!", null, "我知道啦"),
                        new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                            @Override
                            public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                switch (yesOrNoType) {
                                    case BtnOk:
                                        setResult(RESULT_OK);
                                        finish();
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }).show();
                break;

            default:
                break;
        }
    }

    class DriverInfo{
        String id_num;
        String mobile;
        String name;
        String good_num;
        String license;

        public DriverInfo(String id_num, String mobile, String name, String good_num, String license) {
            this.id_num = id_num;
            this.mobile = mobile;
            this.name = name;
            this.good_num = good_num;
            this.license = license;
        }
    }
}
